﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using log4net;
using log4net.Config;
using Ninject.Modules;
using Quartz;
using Topshelf;
using Topshelf.Ninject;
using Topshelf.Quartz;
using Topshelf.Quartz.Ninject;
using Dln.Handler;
using Dln.Model;
using Dln.Common;
using Dln.Common.Helper;

namespace AutoRun
{
    public class SendProblem : IJob
    {
        private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public void Execute(IJobExecutionContext context)
        {
            log4net.Config.XmlConfigurator.Configure();
            Log.InfoFormat("category The current time is: {0}", DateTime.Now);

            var dtTypeList = SqlHelper.ContextDlnOnline.FromSql("SELECT * FROM dt_order with(nolock) where state=0 ").ToList<dt_order>();
            if (dtTypeList != null && dtTypeList.Count() > 0)
            {
                foreach (var item in dtTypeList)
                {
                    if (!string.IsNullOrEmpty(item.AppointUser))
                    {
                        var weChatUsersList = SqlHelper.Context.From<WeChatUsers>().Where(d => d.CardID == item.AppointUser).ToFirst();
                        if (weChatUsersList != null)
                        {
                            Log.InfoFormat(weChatUsersList.Openid + item.id.ToString());
                            MessageHelper.ProblemNotice(weChatUsersList.Openid,
                                            "问题报修",
                                          item.add_time,
                                           "问题报修",
                                           "标题:" + item.title,
                                            "http://www.healthyperson.cn/Page/AutoRun/SendProblem.aspx?Id=" + item.id + "&cardId=" + item.AppointUser
                                        );
                        }

                    }
                    else
                    {
                        var weChatUsersList = SqlHelper.Context.FromSql(string.Format("select * from WeChatUsers where charindex(',{0},',',' + Category + ',')>0 ", item.type)).ToList<WeChatUsers>();

                        if (weChatUsersList != null && weChatUsersList.Count > 0)
                        {
                            foreach (var childItem in weChatUsersList)
                            {
                                if (!string.IsNullOrEmpty(childItem.CardID))
                                {
                                    Log.InfoFormat(childItem.Openid + item.id.ToString());
                                    MessageHelper.ProblemNotice(childItem.Openid,
                                                    "问题报修",
                                                  item.add_time,
                                                   "问题报修",
                                                   "标题：" + item.title,
                                                    "http://www.healthyperson.cn/Page/AutoRun/SendProblem.aspx?Id=" + item.id + "&cardId=" + childItem.CardID
                                                );
                                }
                            }
                        }
                    }
                }
            }

            Log.InfoFormat("结束");
        }
    }
}
